package com.ucode.tool.disruptor;

import com.lmax.disruptor.ExceptionHandler;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * Disruptor自定义异常处理
 * @author: liliang
 * @date: 2020年3月1日 下午12:21:26
 */
public class DisruptorExceptionHandler implements ExceptionHandler<MessageEvent>{

    private static Log log = LogFactory.get();
    
    private DisruptorHandleService disruptorHandleService;
    
    public DisruptorExceptionHandler(DisruptorHandleService disruptorHandleService){
        this.disruptorHandleService = disruptorHandleService;
    }
    
    @Override
    public void handleEventException(Throwable ex, long sequence, MessageEvent event) {
        //事件处理异常里面进行补偿执行
        disruptorHandleService.execute(event.getData());
        log.error(">>> Disruptor事件处理异常，进行立即执行补偿操作..........");
        log.error(">>> 异常信息如下：", ex.getMessage());
    }

    @Override
    public void handleOnStartException(Throwable ex) {
        log.error(">>> Disruptor 启动异常：{}", ex.getMessage());
    }

    @Override
    public void handleOnShutdownException(Throwable ex) {
        log.error(">>> Disruptro 关闭异常：{}", ex.getMessage());
    }

}
